<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
<!--          <a-col :span="24">-->
<!--            <a-form-item label="货主名称" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              &lt;!&ndash; <a-input v-decorator="['ownerName', validatorRules.ownerName]" placeholder="请输入货主名称"  ></a-input> &ndash;&gt;-->
<!--              <a-select v-decorator="['ownerName', validatorRules.ownerName]" placeholder="请选择货主名称">-->
<!--                <a-select-option v-for="item in ownerList" :value="item">{{ item }}</a-select-option>-->
<!--              </a-select>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
          <!-- <a-col :span="24">
            <a-form-item label="货主编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['ownerCode']" placeholder="请输入货主编码"  ></a-input>
            </a-form-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-item label="策略名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['rulesName', validatorRules.rulesName]" placeholder="请输入策略名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="生效日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date :show-time="true" placeholder="请选择生效日期" date-format="YYYY-MM-DD HH:mm:ss"
                v-decorator="['beginTime', validatorRules.beginTime]" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="失效日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date :show-time="true" placeholder="请选择失效日期" date-format="YYYY-MM-DD HH:mm:ss"
                v-decorator="['endTime', validatorRules.endTime]" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-item label="货主互斥" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              &lt;!&ndash; <a-input-number v-decorator="['onwerMutex']" placeholder="请输入货主互斥" style="width: 100%" /> &ndash;&gt;-->
<!--              <j-dict-select-tag type="list" v-decorator="['onwerMutex']" :trigger-change="true"-->
<!--                dictCode="onwerMutex_code" placeholder="请选择货主互斥" />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-item label="货主互斥维度" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              &lt;!&ndash; <a-input-number v-decorator="['onwerMutexRange']" placeholder="请输入货主互斥维度" style="width: 100%" /> &ndash;&gt;-->
<!--              <j-dict-select-tag type="list" v-decorator="['onwerMutexRange']" :trigger-change="true"-->
<!--                dictCode="onwerMutexRange_code" placeholder="请选择货主互斥维度" />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-item label="品番互斥" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- <a-input-number v-decorator="['skuMutex']" placeholder="请输入品番互斥" style="width: 100%" /> -->
              <j-dict-select-tag type="list" v-decorator="['skuMutex']" :trigger-change="true" dictCode="onwerMutex_code"
                placeholder="请选择品番互斥" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="品番互斥维度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- <a-input-number v-decorator="['skuMutexRange']" placeholder="请输入品番互斥维度" style="width: 100%" /> -->
              <j-dict-select-tag type="list" v-decorator="['skuMutexRange']" :trigger-change="true"
                dictCode="onwerMutexRange_code" placeholder="请选择品番互斥维度" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="货架优先级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- <a-input-number v-decorator="['shelfPriority']" placeholder="请输入货架优先级" style="width: 100%" /> -->
              <j-dict-select-tag type="list" v-decorator="['shelfPriority']" :trigger-change="true"
                dictCode="shelfPriority_code" placeholder="请选择货架优先级" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="入库任务优先级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['inOrderPriority']" placeholder="请输入入库任务优先级(数字越大，优先级越高)"
                style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'InOrderRulesForm',
  components: {
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => { },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      ownerList: [], //客户列表
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        // ownerName: {
        //   rules: [
        //     { required: true, message: '请输入货主名称!' },
        //   ]
        // },
        rulesName: {
          rules: [
            { required: true, message: '请输入策略名称!' },
          ]
        },
        beginTime: {
          rules: [
            { required: true, message: '请输入生效日期!' },
          ]
        },
        endTime: {
          rules: [
            { required: true, message: '请输入失效日期!' },
          ]
        },
      },
      url: {
        add: "/inOrderRules/inOrderRules/add",
        edit: "/inOrderRules/inOrderRules/edit",
        queryById: "/inOrderRules/inOrderRules/queryById",
        ownerList: "/basicCustomer/basicCustomer/list",
      }
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData();
    this.getOwnerList();
  },
  methods: {
    //获取货主列表
    getOwnerList() {
      var that = this;
      getAction(this.url.ownerList).then((res) => {
        res.result.records.forEach(element => {
          that.ownerList.push(element.name);
        });
      }).catch((error) => {
        console.error(error);
      });
    },
    add() {
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'ownerName', 'ownerCode', 'rulesName', 'beginTime', 'endTime', 'onwerMutex', 'onwerMutexRange', 'skuMutex', 'skuMutexRange', 'shelfPriority', 'inOrderPriority'))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId };
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result);
          }
        });
      }
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          console.log("表单提交数据", formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'ownerName', 'ownerCode', 'rulesName', 'beginTime', 'endTime', 'onwerMutex', 'onwerMutexRange', 'skuMutex', 'skuMutexRange', 'shelfPriority', 'inOrderPriority'))
    },
  }
}
</script>